
package com.assistant.codegen.modules.model.adapter.web;

import com.assistant.codegen.modules.model.app.ModelService;
import com.assistant.codegen.modules.model.app.cmd.ModelInsertCmd;
import com.assistant.codegen.modules.model.app.cmd.ModelUpdateCmd;
import com.assistant.codegen.modules.model.domain.Model;
import com.assistant.framework.PageUtils;
import com.assistant.util.MessageBodyWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/codegen")
public class ModelController {

    @Autowired
    private ModelService modelService;

    @GetMapping(value = "/model/{id}")
    public MessageBodyWrapper<Model> get(@PathVariable("id") Long id) {
        return MessageBodyWrapper.success(modelService.get(id));
    }

    @GetMapping(value = "/models")
    public MessageBodyWrapper<List<Model>> list() {
        if (PageUtils.isPageRequest()) {
            return MessageBodyWrapper.success(PageUtils.buildToResponse(modelService.page()));
        } else {
            return MessageBodyWrapper.success(modelService.list());
        }
    }

    @PostMapping(value = "/model")
    public MessageBodyWrapper<Model> insert(@RequestBody ModelInsertCmd cmd) {
        return MessageBodyWrapper.success(modelService.insert(cmd));
    }

    @PutMapping(value = "/model/{id}")
    public MessageBodyWrapper<Object> update(@PathVariable("id") Long id, @RequestBody ModelUpdateCmd cmd) {
        cmd.setId(id);
        modelService.update(cmd);
        return MessageBodyWrapper.success();
    }

    @DeleteMapping(value = "/model/{id}")
    public MessageBodyWrapper<Object> delete(@PathVariable("id") Long id) {
        modelService.delete(id);
        return MessageBodyWrapper.success();
    }
}

